<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use App\Models\ChinaArea;
use Encore\Admin\Controllers\AuthController as BaseAuthController;

class AuthController extends BaseAuthController
{
    
    
   /**
     * Model-form for user setting.
     *
     * @return Form
     */
    public function settingForm()
    {

        $form = new Form(new \App\Models\User());
        
        $form->disableEditingCheck();
        $form->disableCreatingCheck();    
        $form->disableViewCheck();        
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
        });
        
        $form->setAction(admin_base_path('auth/setting'));
        $form->tab('基本信息', function (Form $form) {
            $form->display('id', 'ID');
            $form->display('username', trans('admin.username'));
            $form->text('name', __('admin.name'))
                ->creationRules(['required', "unique:users"])
                ->updateRules(['required', "unique:users,name,{{id}}"]);
            $form->email('email', 'Email')			
                ->default(function ($form) {
                    return $form->model()->email;
                })
                ->creationRules(['email',"nullable","unique:users"])
                ->updateRules(['email',"nullable","unique:users,email,{{id}}"]);
            $form->mobile('mobile', '手机')
                ->default(function ($form) {
                    return $form->model()->mobile;
                })
                ->creationRules(["nullable","unique:users"])
                ->updateRules(["nullable","unique:users,mobile,{{id}}"]);
            $form->image('avatar', trans('admin.avatar'))->move('avatar/'.date('Y-m-d'));
            $form->password('password', trans('admin.password'))
                ->default(function ($form) {
                    return $form->model()->password;
                })
                ->rules('required|confirmed');
            $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });

            
            $form->display('backend','后台');
            $form->display('created_at', trans('admin.created_at'));
            $form->display('updated_at', trans('admin.updated_at'));

        })->tab('附加信息', function (Form $form) {
			$form->radio('profile.gender','性别')->options(['f' => '女', 'm' => '男']);
            $form->date('profile.birthday','生日');
            $form->select('profile.province_id','省份')->options(
                ChinaArea::province()->pluck('name', 'id')
            )
            ->load('profile.city_id', admin_base_path('api/china/city'));

            $form->select('profile.city_id','城市')->options(function ($id) {
                return ChinaArea::options($id);
            })->load('profile.district_id', admin_base_path('api/china/district'));

            $form->select('profile.district_id','地区')->options(function ($id) {
                return ChinaArea::options($id);
            });
            $form->text('profile.address','地址');
			$form->url('profile.homepage','主页');
            $form->textarea('profile.document','签名');
        })->tab('社会化', function (Form $form) {
            $form->text('sns.qq');
            $form->text('sns.wechat');
            $form->text('sns.weibo');
            $form->text('sns.github');
            $form->text('sns.google');
            $form->text('sns.facebook');
            $form->text('sns.twitter');
        });

        $form->ignore(['password_confirmation']);

        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });

        $form->saved(function () {
            admin_toastr(trans('admin.update_succeeded'));
            return redirect(admin_base_path('auth/setting'));
        });

        return $form;
    }

}
